package com.olympia.myapp.xml;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.olympia.myapp.entity.Player;

public class LayerTopXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = null;
	public List<Player> rsPlayers = null;
	Player player = null;
	String fullName = "";
	String email = "";
	int id = 0;

	/**
	 * This XML file does not appear to have any style information associated
	 * with it. The document tree is shown below. <AZSOFT> <OLPA> <item>
	 * <fullname>vvn</fullname> <email>vvn@gmail.com</email> </item> <item>
	 * <fullname>tatuanvn</fullname> <email>tatuanvn@gmail.com</email> </item>
	 * <item> <fullname>tthop</fullname> <email>tthop@gmail.com</email> </item>
	 * <item> <fullname>Hoangtuyet1234</fullname>
	 * <email>Hoangtuyet1234@yahoo.com</email> </item> <item>
	 * <fullname>Thuyanh1496</fullname> <email>Thuyanh1496@yahoo.com</email>
	 * </item> <item> <fullname>Viet113</fullname>
	 * <email>Viet113@yahoo.com</email> </item> <item>
	 * <fullname>Hacochp1</fullname> <email>Hacochp1@yahoo.com</email> </item>
	 * <item> <fullname>Dattunn</fullname> <email>Dattunn@yahoo.com</email>
	 * </item> <item> <fullname>Nguyenngocha</fullname>
	 * <email>Nguyenngocha@gmail.com</email> </item> <item>
	 * <fullname>Hoangviet113</fullname> <email>Hoangviet113@yahoo.com</email>
	 * </item> </OLPA> </AZSOFT>
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currentElement = true;

		if (localName.equals("OLPA")) {
			/** Start */
			rsPlayers = new ArrayList<Player>();
		}
	}

	/*
	 * 
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		/** set value */
		if (localName.equalsIgnoreCase("fullname"))
			fullName = currentValue;
		else if (localName.equalsIgnoreCase("email"))
			email = currentValue;
		else if (localName.equalsIgnoreCase("item")) {
			id += 1;
			player = new Player(id, fullName, email, "0", "0", "0", "0", "0");
			rsPlayers.add(player);
		}

	}

	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}

}
